package com.imau.controller;

import com.alibaba.fastjson.JSON;
import com.imau.bean.Permission;
import com.imau.service.PermissionService;
import com.imau.service.impl.PermissionServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@WebServlet(name = "PermissionController", value = "/permission/*")
public class PermissionController extends Baseconterllor {


    private PermissionService permissionService = new PermissionServiceImpl();


    /**
     * 添加教师
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


        //前端JSON对象
        String jsonString = request.getReader().readLine();

        //转成对象
        //转换Map对象
        Map map = JSON.parseObject(jsonString, Map.class);

        //3.保存
        int row = permissionService.savePermission(map);

        //4.返回响应

        this.writeResult(response, row > 0 ? true : false);


    }


    /**
     * 删除
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void removeById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


        //前端JSON对象
        String id = request.getParameter("id");


        //删除
        int row = permissionService.deleteByPermissionId(id);
        //4.返回响应

        this.writeResult(response, row > 0 ? true : false);


    }


    protected void edit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


        //前端JSON对象
        String jsonString = request.getReader().readLine();


        //转换Map对象
        Map map = JSON.parseObject(jsonString, Map.class);

        //3.保存
        int row = permissionService.updatePermission(map);

        //4.返回响应

        this.writeResult(response, row > 0 ? true : false);


    }


    /**
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


        //前端JSON对象
        String jsonString = request.getReader().readLine();

        //转成对象
        Permission permission = JSON.parseObject(jsonString, Permission.class);



        //3.保存

        List<Permission> allPermission = permissionService.findAllPermission(permission);

        //4.返回响应
        writeResultAndData(response, allPermission);


    }


    /**
     * 菜单查询
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void listMenu(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


        //前端JSON对象
        String jsonString = request.getReader().readLine();

        //转成对象
        Permission permission = JSON.parseObject(jsonString, Permission.class);


        if (permission == null)
        {
            permission = new Permission();
        }

        //对象构建
        permission.setType(0);

        //3.保存

        List<Permission> allPermission = permissionService.findAllPermission(permission);

        //4.返回响应
        writeResultAndData(response, allPermission);


    }

    protected void getInfo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


        String id = request.getParameter("id");


        Map<String, Object> permissionMap = permissionService.findByPermissionId(id);

        //4.返回响应
        writeResultAndData(response, permissionMap);


    }
}
